package com.asktgapp.user.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.asktgapp.JGTool.JGColor;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.JGTool.Utils;
import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.model.ConsumVO;
import com.asktgapp.widget.RefreshRecyclerView;
import com.asktgapp.modulebase.common.adapter.BaseAdapter;
import com.asktgapp.modulebase.common.adapter.BaseViewHolder;
import com.asktgapp.modulebase.common.util.PreferencesUtil;

import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/1/8
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:通知中心模块下的消费记录
 */

public class ConsumptionRecordFragment extends BaseFragment {
//    @InjectView(R.id.recycle)
    RefreshRecyclerView mRefreshRecyclerView;
    private RecyclerView mRecycerView;
    private BaseAdapter mBaseAdapter;
    private int mPage = 1;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_consumption_record, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        mRefreshRecyclerView=rootView.findViewById(R.id.recycle);
        mRecycerView = mRefreshRecyclerView.getRecyclerView();

        mRefreshRecyclerView.setLoadMoreAction(new RefreshRecyclerView.Action() {
            @Override
            public void onAction(int page) {
                mPage = page;
                getData();
            }
        });
        mRefreshRecyclerView.setRefreshAction(new RefreshRecyclerView.Action() {
            @Override
            public void onAction(int page) {
                mPage = page;
                getData();
            }
        });
        mBaseAdapter = new BaseAdapter(getActivity()) {
            @Override
            public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
                BaseViewHolder holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_consumption, parent, false),
                        new BaseViewHolder.Callbacks() {
                            @Override
                            public void bind(BaseViewHolder holder, Object data) {
//                                LinearLayout bg = holder.getView(R.id.ll_bg);
                                if (data instanceof ConsumVO) {
                                    ConsumVO bean = (ConsumVO) data;
                                    String title;
                                    if (bean.getInfo().contains("-")) {
                                        int index = bean.getInfo().lastIndexOf("-");
                                        title = bean.getInfo().substring(index + 1) + "消费";
                                    } else {
                                        title = bean.getInfo();
                                    }
                                    holder.setText(R.id.tv_consumption_item_title,"编号："+bean.getOrderNum()+"\n"+ title);
                                    holder.setText(R.id.tv_consumption_item_content, bean.getInfo());
                                    String payType = bean.getPayType()==0?"支付宝":"微信";
                                    String payStatus = bean.getStatus()==0?"已支付":"未支付";
                                    if (bean.getMode() == 0) {
                                        holder.setText(R.id.tv_consumption_item_money, payType+"\n+" + String.valueOf(bean.getMoney())+"\n"+payStatus);
                                    } else {
                                        holder.setText(R.id.tv_consumption_item_money, payType+"\n-" + String.valueOf(bean.getMoney())+"\n"+payStatus);
                                    }
                                    holder.setText(R.id.tv_consumption_item_time, Utils.getDateAll(bean.getAddTime()));
                                }

                            }

                            @Override
                            public void onItemClick(View view, int position) {

                            }
                        });
                return holder;
            }
        };
        mRecycerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecycerView.setAdapter(mBaseAdapter);

    }

    @Override
    public void loadData() {
        visibleLoading();
        getData();

    }

    private void getData() {
        // TODO: 2018/1/8  分页的网络请求
        HashMap params = new HashMap();
        params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
//        params.put("page", mPage);
        Call<ApiResponseBody<List<ConsumVO>>> call = ApiUtil.getInstance()
                .create(Apiservice.class).getCashMsg(params);
        call.enqueue(new Callback<ApiResponseBody<List<ConsumVO>>>() {
            @Override
            public void onResponse(Call<ApiResponseBody<List<ConsumVO>>> call, Response<ApiResponseBody<List<ConsumVO>>> response) {
                mRefreshRecyclerView.endPage();

                inVisibleLoading();
                if (response.isSuccessful()) {
                    List<ConsumVO> list = response.body().getResult();
                    if (list.size() <= 0) {
                        visibleNoData();
                    } else {
                        mBaseAdapter.setData(list);
                        mRefreshRecyclerView.showNoMore();
                    }
//                    if(mPage==1) {
//
//                    }else {
//                        if (list.size() <= 0) {
//                            mRefreshRecyclerView.showNoMore();
//                        } else {
//                            mBaseAdapter.addData(BaseAdapter.AddType.LASE,list);
//                            mRefreshRecyclerView.showNextMore();
//                        }
//                    }
                } else {
                    showTost(response.raw().message(), 0);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody<List<ConsumVO>>> call, Throwable t) {
                mRefreshRecyclerView.endPage();
                inVisibleLoading();
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 1);
                }

            }
        });
        addSubscription(call);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
